/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tools.idea.insights.events

import com.android.tools.idea.insights.AppInsightsState
import com.android.tools.idea.insights.InsightsProvider
import com.android.tools.idea.insights.LoadingState
import com.android.tools.idea.insights.analytics.AppInsightsTracker
import com.android.tools.idea.insights.client.AppInsightsCache
import com.android.tools.idea.insights.events.actions.Action
import com.android.tools.idea.insights.experiments.InsightFeedback

class InsightFeedbackSubmitted(val feedback: InsightFeedback) : ChangeEvent {
  override fun transition(
    state: AppInsightsState,
    tracker: AppInsightsTracker,
    provider: InsightsProvider,
    cache: AppInsightsCache,
  ): StateTransition<Action> {
    val connection = state.connections.selected ?: return state.toEmptyTransition()
    val currentIssue = state.selectedIssue ?: return state.toEmptyTransition()
    val currentInsight = state.currentInsight.valueOrNull() ?: return state.toEmptyTransition()
    val newInsight = currentInsight.copy(feedback = feedback)
    val newState = state.copy(currentInsight = LoadingState.Ready(newInsight))
    tracker.logInsightSentiment(
      feedback.toSentiment(),
      currentIssue.issueDetails.fatality.toCrashType(),
      currentInsight,
    )
    cache.putAiInsight(connection, currentIssue.id, state.selectedVariant?.id, newInsight)
    return StateTransition(newState, Action.NONE)
  }
}
